package com.leyou.item.web;

import com.baomidou.mybatisplus.extension.conditions.query.QueryChainWrapper;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.leyou.commons.dto.PageDTO;
import com.leyou.item.dto.BrandDTO;
import com.leyou.item.entity.Brand;
import com.leyou.item.entity.Category;
import com.leyou.item.service.BrandService;
import org.hibernate.validator.internal.metadata.aggregated.rule.VoidMethodsMustNotBeReturnValueConstrained;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.awt.*;
import java.util.List;

@RestController
@RequestMapping("/brand")
public class BrandController {
    @Autowired
    private BrandService brandService;

    /**todo:
     * 根据品牌id查询品牌
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public ResponseEntity<BrandDTO> queryBrandById(@PathVariable("id") long id){
      return ResponseEntity.ok(new BrandDTO(this.brandService.getById(id)));
    }

    /**todo:
     * 根据品牌id集合查询品牌集合
     * @param ids
     * @return
     */
    @GetMapping("/list")
    public ResponseEntity<List<BrandDTO>> queryBrandByIds(@RequestParam("ids")List<Long> ids){
        return  ResponseEntity.ok(BrandDTO.convertEntityList(this.brandService.listByIds(ids)));
    }

    /**todo:
     * 根据条件分页查询
     * @param key
     * @param page
     * @param rows
     * @return
     */
    @GetMapping("/page")
    public ResponseEntity<PageDTO<BrandDTO>> queryBrandByPage(@RequestParam(value = "key",required = false)String key,
                                                              @RequestParam(value = "page",defaultValue = "1") Integer page,
                                                              @RequestParam(value = "rows",defaultValue = "5")Integer rows){
        return ResponseEntity.ok(this.brandService.queryBrandByPage(page,rows,key));
    }

    /**todo:
     * 根据分类id查询品牌集合
     * @return
     */
    @GetMapping("/of/category")
    public ResponseEntity<List<BrandDTO>> queryBrandByCategory(@RequestParam("id")Integer id){
        return ResponseEntity.ok(this.brandService.queryBrandByCategory(id));
    }

    /**todo：
     * 根据id删除品牌
     * @param Id
     * @return
     */
    @DeleteMapping("{id}")
    public ResponseEntity<Void> deleteBrandById(@PathVariable("id") Integer Id){
        this.brandService.deleteBrandById(Id);
        return new  ResponseEntity(HttpStatus.NO_CONTENT);
    }

    /**todo：
     * 新增品牌
     * @param brandDTO
     * @return
     */
    @PostMapping
    public ResponseEntity<Void> addBrand(BrandDTO brandDTO){
    this.brandService.addbrand(brandDTO);
    return new ResponseEntity(HttpStatus.CREATED);
    }

    /**todo
     * 修改品牌信息
     * @param brandDTO
     * @return
     */
    @PutMapping
    public ResponseEntity<Void> updateBrand(BrandDTO brandDTO){
    this.brandService.updateBrand(brandDTO);
    return new ResponseEntity(HttpStatus.NO_CONTENT);
    }
}
